package com.imooc.ecommerce.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.imooc.ecommerce.block_handler.CustomBlockHandler;
import com.imooc.ecommerce.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 给予Sentinel dashboard 控制台配置流控规则
 * Sentinel 是懒加载的,先去访问一下,就可以在Sentinel Dashboard中看到
 * @author zhangshao
 * @since 2023/12/6 20:24
 */
@Slf4j
@RestController
@RequestMapping("/dashboard")
public class RateLimitController {

    /**
     * 在dashboard中 "流控规则"中按照资源名称新增流控规则
     */
    @GetMapping("/by-resource")
    @SentinelResource(value = "byResource",blockHandler = "commonHandlerBlockException",blockHandlerClass = CustomBlockHandler.class)
    public CommonResponse<String> byResource(){
        log.info("coming in rate limit by resource");
        return new CommonResponse<>(0,"","byResource");
    }

    /**
     * 在“簇点链路”中给url添加流控规则
     */
    @GetMapping("/by-url")
    @SentinelResource(value = "byUrl")
    public CommonResponse<String> byUrl(){
        log.info("coming in rate limit controller by url");
        return new CommonResponse<>(0,"","byUrl");

    }
}
